package com.kjj.gift.holder;

import com.alibaba.fastjson.JSONObject;
import com.kjj.gift.context.LoginUserContextInfo;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

public class LoginUserContextInfoHolder {

    /**
     * 获取登陆用户上下文信息
     * @return
     */
    public static LoginUserContextInfo getLoginUserContextInfo(){
        //获取登陆用户
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();

        //从上下文中获取用户主体信息
        String principal = (String) authentication.getPrincipal();
        LoginUserContextInfo info = JSONObject
                .parseObject(principal, LoginUserContextInfo.class);
        return info;
    }
}
